package com.globallogic.week3.config;


import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;


public class JettyServer {
	private static final int PORT = 8090;
	private static final String CONTEXT_PATH = "/week3";
	private static final String HOST = "localhost";
	
	private Server jetty;
	
	public void start(){
		jetty = new Server(PORT);
		
		WebAppContext context = new WebAppContext();
		context.setResourceBase("target/week3");
		context.setContextPath(CONTEXT_PATH);
		context.setServer(jetty);
		
		context.setConfigurations(new Configuration[]{
				new AnnotationConfiguration(), new WebXmlConfiguration(),
	            new WebInfConfiguration()
		});

		jetty.setHandler(context);
		jetty.setStopAtShutdown(true);
		
		try {
			jetty.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void stop(){
		try {
			jetty.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getURL(String path){
		
		return String.format("http://%s:%d%s%s", HOST,PORT,CONTEXT_PATH,path);
	}

}
